#include "ShapeOperation.h"

#include "Core/RWTools.h"
#include "Serialize/SerializeHelper.h"

ShapeOperation* ShapeOperationFactory::CreateOperation(EOperationType operationType)
{
	switch (operationType)
	{
	case EOperationType::Create:
		return new CreateShapeOperation;
	case EOperationType::Boolean:
		return new BooleanOperation;
	case EOperationType::Delete:
		return new DeleteShapeOperation;
	case EOperationType::Transform:
		return new TransformOperation;
	case EOperationType::Hollow:
		return new HollowOperation;
	case EOperationType::Stretch:
		return new StretchOperation;
	case EOperationType::Revolve:
		return new RevolveOperation;
	case EOperationType::RoundOff:
		return new RoundOffOperation;
	case EOperationType::Sketch:
		return new SketchOperation;
	case EOperationType::FillFace:
        return new FillFaceOperation;
	case EOperationType::CreateFromObject:
		return new CreateShapeFromObjectOperatopn;
	case EOperationType::DetachEdge:
        return new DetachEdgeOperation;
	case EOperationType::DetachFace:
        return new DetachFaceOperation;
	case EOperationType::UncoverFace:
        return new UncoverFaceOperation;
	case EOperationType::CoverFace:
		return new CoverFaceOperation;
	case EOperationType::MoveEdge:
        return new MoveEdgeOperation;
	case EOperationType::MoveFace:
        return new MoveFaceOperation;
	case EOperationType::Section:
		return new SectionOperation;
	}

	std::cout << "ShapeOperationFactory::CreateOperation---->Invalid operation type" << std::endl;
	return nullptr;
}

void ShapeOperation::serialize(std::ofstream& out) const
{
	// 写入操作类型
	out.write(reinterpret_cast<const char*>(&m_operationType), sizeof(m_operationType));

	// 使用 UTF-8 编码转换 QString 到 std::string
	QByteArray utf8Data = operationName.toUtf8();
	int nameLength = utf8Data.size();

	// 写入长度
	out.write(reinterpret_cast<const char*>(&nameLength), sizeof(nameLength));

	// 写入 UTF-8 编码的数据
	out.write(utf8Data.constData(), nameLength);
}

void ShapeOperation::deserialize(std::ifstream& in)
{
	// 读取长度
	int nameLength = 0;
	in.read(reinterpret_cast<char*>(&nameLength), sizeof(nameLength));

	// 读取 UTF-8 编码的数据
	char* nameBuffer = new char[nameLength + 1];
	in.read(nameBuffer, nameLength);
	nameBuffer[nameLength] = '\0';

	// 使用 UTF-8 解码 std::string 到 QString
	operationName = QString::fromUtf8(nameBuffer);
	delete[] nameBuffer;
}

std::shared_ptr<FeatureShape> ShapeOperation::deserializeFeatureShape(std::ifstream& in)
{
	// 读取 FeatureShape 数据
	EFeatureType featureType = EFeatureType::None;
	in.read(reinterpret_cast<char*>(&featureType), sizeof(featureType));
	std::shared_ptr<FeatureShape> featureShape = FeatureShapeFactory::CreateFeatureShape(featureType);
	featureShape->deserialize(in);

	return featureShape;
}

void CreateShapeOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	m_featureShape = deserializeFeatureShape(in);
}

void CreateShapeOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	// 写入 FeatureShape 数据
	m_featureShape->serialize(out);
}

void BooleanOperation::deserialize(std::ifstream& in)
{
    ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_targetShape), sizeof(m_targetShape));
	in.read(reinterpret_cast<char*>(&m_toolShape), sizeof(m_toolShape));
	in.read(reinterpret_cast<char*>(&m_booleanStyle), sizeof(m_booleanStyle));
}

void BooleanOperation::serialize(std::ofstream& out) const
{
    ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_targetShape), sizeof(m_targetShape));
	out.write(reinterpret_cast<const char*>(&m_toolShape), sizeof(m_toolShape));
	out.write(reinterpret_cast<const char*>(&m_booleanStyle), sizeof(m_booleanStyle));
}

void DeleteShapeOperation::deserialize(std::ifstream& in)
{
    ShapeOperation::deserialize(in);

	size_t indexSize;
	in.read(reinterpret_cast<char*>(&indexSize), sizeof(indexSize));
	m_shapeIndex.resize(indexSize);
	in.read(reinterpret_cast<char*>(m_shapeIndex.data()), indexSize * sizeof(int));
}

void DeleteShapeOperation::serialize(std::ofstream& out) const
{
    ShapeOperation::serialize(out);

	size_t indexSize = m_shapeIndex.size();
	out.write(reinterpret_cast<const char*>(&indexSize), sizeof(indexSize));
	out.write(reinterpret_cast<const char*>(m_shapeIndex.data()), indexSize * sizeof(int));
}

void TransformOperation::deserialize(std::ifstream& in)
{
    ShapeOperation::deserialize(in);

	// 读取 shapeIndex 的大小和数据
	size_t indexSize;
	in.read(reinterpret_cast<char*>(&indexSize), sizeof(indexSize));
	m_shapeIndex.resize(indexSize);
	in.read(reinterpret_cast<char*>(m_shapeIndex.data()), indexSize * sizeof(int));

	in >> m_transform;
}

void TransformOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	// 写入 shapeIndex 的大小和数据
	size_t indexSize = m_shapeIndex.size();
	out.write(reinterpret_cast<const char*>(&indexSize), sizeof(indexSize));
	out.write(reinterpret_cast<const char*>(m_shapeIndex.data()), indexSize * sizeof(int));

	out << m_transform;
}

void StretchOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));

	// 读取 subShapeIndex 的大小和数据
	size_t indexSize;
	in.read(reinterpret_cast<char*>(&indexSize), sizeof(indexSize));
	m_subShapeIndex.resize(indexSize);
	in.read(reinterpret_cast<char*>(m_subShapeIndex.data()), indexSize * sizeof(int));

	in >> m_stretchVector;

	in.read(reinterpret_cast<char*>(&m_bIsEdge), sizeof(m_bIsEdge));
}

void StretchOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));

	// 写入 subShapeIndex 的大小和数据
	size_t indexSize = m_subShapeIndex.size();
	out.write(reinterpret_cast<const char*>(&indexSize), sizeof(indexSize));
	out.write(reinterpret_cast<const char*>(m_subShapeIndex.data()), indexSize * sizeof(int));

	out << m_stretchVector;

	out.write(reinterpret_cast<const char*>(&m_bIsEdge), sizeof(m_bIsEdge));
}

void HollowOperation::deserialize(std::ifstream& in)
{
    ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	in.read(reinterpret_cast<char*>(&m_thickness), sizeof(m_thickness));

	// 读取 subFaceIndex 的大小和数据
	size_t indexSize;
	in.read(reinterpret_cast<char*>(&indexSize), sizeof(indexSize));
	m_subFaceIndex.resize(indexSize);
	in.read(reinterpret_cast<char*>(m_subFaceIndex.data()), indexSize * sizeof(int));
}

void HollowOperation::serialize(std::ofstream& out) const
{
    ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	out.write(reinterpret_cast<const char*>(&m_thickness), sizeof(m_thickness));

	// 写入 subFaceIndex 的大小和数据
	size_t indexSize = m_subFaceIndex.size();
	out.write(reinterpret_cast<const char*>(&indexSize), sizeof(indexSize));
	out.write(reinterpret_cast<const char*>(m_subFaceIndex.data()), indexSize * sizeof(int));
}

void RevolveOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));

	// 读取 subShapeIndex 的大小和数据
	size_t indexSize;
	in.read(reinterpret_cast<char*>(&indexSize), sizeof(indexSize));
	m_subShapeIndex.resize(indexSize);
	in.read(reinterpret_cast<char*>(m_subShapeIndex.data()), indexSize * sizeof(int));

	in.read(reinterpret_cast<char*>(&m_angle), sizeof(m_angle));
	in >> m_rotationAxis;
	in.read(reinterpret_cast<char*>(&m_bUseEdge), sizeof(m_bUseEdge));
}

void RevolveOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));

	// 写入 subShapeIndex 的大小和数据
	size_t indexSize = m_subShapeIndex.size();
	out.write(reinterpret_cast<const char*>(&indexSize), sizeof(indexSize));
	out.write(reinterpret_cast<const char*>(m_subShapeIndex.data()), indexSize * sizeof(int));

	out.write(reinterpret_cast<const char*>(&m_angle), sizeof(m_angle));
	out << m_rotationAxis;
	out.write(reinterpret_cast<const char*>(&m_bUseEdge), sizeof(m_bUseEdge));
}

void RoundOffOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));

	// 读取 subShapeIndex 的大小和数据
	size_t indexSize;
	in.read(reinterpret_cast<char*>(&indexSize), sizeof(indexSize));
	m_subShapeIndex.resize(indexSize);
	in.read(reinterpret_cast<char*>(m_subShapeIndex.data()), indexSize * sizeof(int));

	in.read(reinterpret_cast<char*>(&m_radius), sizeof(m_radius));
}

void RoundOffOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));

	// 写入 subShapeIndex 的大小和数据
	size_t indexSize = m_subShapeIndex.size();
	out.write(reinterpret_cast<const char*>(&indexSize), sizeof(indexSize));
	out.write(reinterpret_cast<const char*>(m_subShapeIndex.data()), indexSize * sizeof(int));

	out.write(reinterpret_cast<const char*>(&m_radius), sizeof(m_radius));
}

void SketchOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in >> m_normal >> m_right >> m_center;
	// 读取 m_featureShapes 的大小和数据
	size_t featureShapesSize = m_featureShapes.size();
	in.read(reinterpret_cast<char*>(&featureShapesSize), sizeof(featureShapesSize));
	for (size_t i = 0; i < featureShapesSize; ++i)
	{
		m_featureShapes.push_back(deserializeFeatureShape(in));
	}
}

void SketchOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out << m_normal << m_right << m_center;
	// 写入 m_featureShapes 的大小和数据
	size_t featureShapesSize = m_featureShapes.size();
	out.write(reinterpret_cast<const char*>(&featureShapesSize), sizeof(featureShapesSize));
	for (const auto& featureShape : m_featureShapes)
	{
		featureShape->serialize(out);
	}
}

void FillFaceOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	size_t shapeIndexSize;
	in.read(reinterpret_cast<char*>(&shapeIndexSize), sizeof(shapeIndexSize));
	m_shapeIndex.reserve(shapeIndexSize);
	for (size_t i = 0; i < shapeIndexSize; ++i)
	{
		std::pair<int, std::vector<int>> pair;
		// 读取first
        in.read(reinterpret_cast<char*>(&pair.first), sizeof(pair.first));
		// 读取second
		size_t indexSize;
		in.read(reinterpret_cast<char*>(&indexSize), sizeof(indexSize));
		pair.second.resize(indexSize);
		in.read(reinterpret_cast<char*>(pair.second.data()), indexSize * sizeof(int));
		m_shapeIndex.push_back(pair);
	}
}

void FillFaceOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	// 写入 m_shapeIndex 的大小
    size_t shapeIndexSize = m_shapeIndex.size();
    out.write(reinterpret_cast<const char*>(&shapeIndexSize), sizeof(shapeIndexSize));
	for (int i = 0;i < shapeIndexSize;i++)
	{
		// 写入 first
		out.write(reinterpret_cast<const char*>(&m_shapeIndex[i].first), sizeof(m_shapeIndex[i].first));
		// 写入second
		size_t subShapeIndexSize = m_shapeIndex[i].second.size();
		out.write(reinterpret_cast<const char*>(&subShapeIndexSize), sizeof(subShapeIndexSize));
        out.write(reinterpret_cast<const char*>(m_shapeIndex[i].second.data()), subShapeIndexSize * sizeof(int));

	}
}

void CreateShapeFromObjectOperatopn::deserialize(std::ifstream& in)
{
    ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	in.read(reinterpret_cast<char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
	in.read(reinterpret_cast<char*>(&bUseEdge), sizeof(bUseEdge));
}

void CreateShapeFromObjectOperatopn::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));
    out.write(reinterpret_cast<const char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
    out.write(reinterpret_cast<const char*>(&bUseEdge), sizeof(bUseEdge));
}

void DetachEdgeOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	in.read(reinterpret_cast<char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
}

void DetachEdgeOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	out.write(reinterpret_cast<const char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
}

void DetachFaceOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	in.read(reinterpret_cast<char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
}

void DetachFaceOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	out.write(reinterpret_cast<const char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
}

void UncoverFaceOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	in.read(reinterpret_cast<char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
}

void UncoverFaceOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	out.write(reinterpret_cast<const char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
}

void CoverFaceOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));
}

void CoverFaceOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));
}

void MoveEdgeOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	in.read(reinterpret_cast<char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
	in.read(reinterpret_cast<char*>(&m_moveDistance), sizeof(m_moveDistance));
}

void MoveEdgeOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	out.write(reinterpret_cast<const char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
	out.write(reinterpret_cast<const char*>(&m_moveDistance), sizeof(m_moveDistance));
}

void MoveFaceOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	in.read(reinterpret_cast<char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
	in >> m_moveVec;
}

void MoveFaceOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	out.write(reinterpret_cast<const char*>(&m_subShapeIndex), sizeof(m_subShapeIndex));
	out << m_moveVec;
}

void SectionOperation::deserialize(std::ifstream& in)
{
	ShapeOperation::deserialize(in);

	in.read(reinterpret_cast<char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	in.read(reinterpret_cast<char*>(&m_planeIndex), sizeof(m_planeIndex));
}

void SectionOperation::serialize(std::ofstream& out) const
{
	ShapeOperation::serialize(out);

	out.write(reinterpret_cast<const char*>(&m_shapeIndex), sizeof(m_shapeIndex));
	out.write(reinterpret_cast<const char*>(&m_planeIndex), sizeof(m_planeIndex));
}
